package websocket

import (
	"duoke/service"
	utils "duoke/utils/common"
	"duoke/utils/gconv"
	"duoke/utils/gjwt"
	"encoding/json"
	"errors"
	"github.com/gin-gonic/gin"
)

type Resp struct {
	Account *gjwt.Data
	Data    *map[string]interface{}
}

func Gjwt(req *Request, c *gin.Context) (*Resp, error) {
	data := gconv.String(req.Data["data"])
	key := []byte("Q!fqhthlsFuazZ(gvlWTY(h(n_x*2i4+")
	iv := []byte("Js8K1kDYC^K^FuWZ")
	encrypt, err := utils.Common.AESDecrypt(data, key, iv)
	if err != nil {
		return nil, err
	}
	var m map[string]interface{}
	err = json.Unmarshal([]byte(encrypt), &m)
	if err != nil {
		return nil, err
	}
	token := gconv.String(m["token"])
	if token == "" {
		return nil, errors.New("参数缺失【token】")
	}
	result, err := gjwt.Parse(token)
	if err != nil {
		return nil, err
	}
	//result.Acid = gconv.Int(m["acid"])
	result.Acid = 1
	c.Set("user", service.UserStruct{Id: result.Uid, Acid: result.Acid})
	return &Resp{
		Account: result,
		Data:    &m,
	}, nil
}
